//package org.jeecg.readExcel.service.impl;
//
//import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
//import com.fasterxml.jackson.core.type.TypeReference;
//import org.apache.poi.ss.usermodel.Row;
//import org.apache.poi.ss.usermodel.Sheet;
//import org.apache.poi.ss.usermodel.Workbook;
//import org.apache.poi.xssf.usermodel.XSSFWorkbook;
//import org.jeecg.common.api.vo.Result;
//import org.jeecg.common.util.DateUtils;
//import org.jeecg.common.util.JacksonUtil;
//import org.jeecg.common.util.RedisUtil;
//import org.jeecg.readExcel.entity.TenderInformationEntity;
//import org.jeecg.readExcel.entity.TenderInformationKeywordManagementRelEntity;
//import org.jeecg.readExcel.entity.TenderSecurityKeywordManagementEntity;
////import org.jeecg.readExcel.mapper.TenderSecurityKeywordManagementMapper;
//import org.jeecg.readExcel.service.TenderInformationKeywordManagementRelService;
//import org.jeecg.readExcel.service.TenderSecurityKeywordManagementService;
//import org.springframework.beans.factory.annotation.Autowired;
//import org.springframework.stereotype.Service;
//
//import javax.annotation.Resource;
//import java.io.File;
//import java.io.FileInputStream;
//import java.io.IOException;
//import java.text.ParseException;
//import java.util.ArrayList;
//import java.util.Date;
//import java.util.List;
//import java.util.Map;
//import java.util.stream.Collectors;
//
//
//@Service("tenderSecurityKeywordManagementService")
//public class TenderSecurityKeywordManagementServiceImpl extends ServiceImpl<TenderSecurityKeywordManagementMapper, TenderSecurityKeywordManagementEntity> implements TenderSecurityKeywordManagementService {
//
//    private static final String SECURITY_KEYWORD_MANAGE_CACHE = "SECURITY_KEYWORD_MANAGE_CACHE";
//    private static final long SECURITY_KEYWORD_MANAGE_CACHE_EXPIRE = 3600L;
//
//    @Autowired
//    TenderInformationKeywordManagementRelService tenderInformationKeywordManagementRelService;
//    @Resource
//    private RedisUtil redisUtil;
//
//    public Result<?> read() {
//        List<TenderInformationEntity> entities = new ArrayList<>();
//        List<TenderInformationKeywordManagementRelEntity> managementRelEntities = new ArrayList<>();
//        try (FileInputStream fis = new FileInputStream(new File("/Users/liangfeng/Downloads/招标数据/中国电信阳光采购网/中国电信阳光采购网.xlsx"));
//             Workbook workbook = new XSSFWorkbook(fis)) {
//
//            List<TenderSecurityKeywordManagementEntity> list = this.lambdaQuery().list();
//            // 假设使用 entity 的某个唯一字段（例如 id 或 keyword 作为 key）
//            Map<String, TenderSecurityKeywordManagementEntity> keywordManagMap = list.stream()
//                    .collect(Collectors.toMap(TenderSecurityKeywordManagementEntity::getKeywordName, entity -> entity));
//
//            Sheet sheet = workbook.getSheetAt(0); // 读取第一个Sheet
//            // 跳过表头，从第二行开始
//            for (int i = 1; i <= sheet.getLastRowNum(); i++) {
//                Row row = sheet.getRow(i);
//                if (row == null) continue;
//
//                String docTitle = row.getCell(1).getStringCellValue();
//                String docCode = row.getCell(2).getStringCellValue();
//                String startDate = row.getCell(3).getStringCellValue();
//                String endDate = row.getCell(4).getStringCellValue();
//                String createDate = row.getCell(5).getStringCellValue();
//                String provinceName = row.getCell(6).getStringCellValue();
//                String creatorName = row.getCell(7).getStringCellValue();
//                String docType = row.getCell(8).getStringCellValue();
//                String provinceCode = row.getCell(9).getStringCellValue();
//
//                TenderSecurityKeywordManagementEntity keywordManagement = keywordManagMap.entrySet().stream()
//                        .filter(entry -> docTitle.contains(entry.getKey())) // 模糊匹配
//                        .map(Map.Entry::getValue) // 提取 value
//                        .findFirst() // 取第一个匹配的值
//                        .orElse(null); // 如果找不到，则返回 null
//
//                if(null!=keywordManagement){
//                    TenderInformationEntity entity = new TenderInformationEntity();
//                    entity.setTender_title(docTitle);
//                    entity.setTender_desc(docTitle);
//                    entity.setTender_date(DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss"));
//                    entity.setProject_num(docCode);
//                    entity.setProject_name(docTitle);
//                    entity.setStart_time(DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss"));
//                    entity.setEnd_time(DateUtils.parseDate(endDate, "yyyy-MM-dd HH:mm:ss"));
//                    entity.setWebsite_url("https://caigou.chinatelecom.com.cn/search");
//                    entity.setCrawl_time(new Date());
//                    entity.setCreate_by("system");
//                    entity.setCreate_time(new Date());
////                    tenderInformationService.save(entity);
////                    entities.add(entity);
//
//                    TenderInformationKeywordManagementRelEntity relEntity = new TenderInformationKeywordManagementRelEntity();
//                    relEntity.setInformation_id(String.valueOf(entity.getId()));
//                    relEntity.setSecurity_keyword_management_id(String.valueOf(keywordManagement.getId()));
//                    relEntity.setCreate_by("system");
//                    relEntity.setCreate_time(new Date());
//                    managementRelEntities.add(relEntity);
//                }
//
////                if(docTitle.contains("采购项目")){
////                    TenderSecurityKeywordManagementEntity keywordManagement =
////                            this.lambdaQuery().eq(TenderSecurityKeywordManagementEntity::getKeyword_name, "采购项目").one();
////
////                    TenderInformationEntity entity = new TenderInformationEntity();
////                    entity.setTender_title(docTitle);
////                    entity.setTender_desc(docTitle);
////                    entity.setTender_date(DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss"));
////                    entity.setProject_num(docCode);
////                    entity.setProject_name(docTitle);
////                    entity.setStart_time(DateUtils.parseDate(startDate, "yyyy-MM-dd HH:mm:ss"));
////                    entity.setEnd_time(DateUtils.parseDate(endDate, "yyyy-MM-dd HH:mm:ss"));
////                    entity.setWebsite_url("https://caigou.chinatelecom.com.cn/search");
////                    entity.setCrawl_time(new Date());
////                    entity.setCreate_by("system");
////                    entity.setCreate_time(new Date());
////                    tenderInformationService.save(entity);
//////                    entities.add(entity);
////
////                    TenderInformationKeywordManagementRelEntity relEntity = new TenderInformationKeywordManagementRelEntity();
////                    relEntity.setInformation_id(String.valueOf(entity.getId()));
////                    relEntity.setSecurity_keyword_management_id(String.valueOf(keywordManagement.getId()));
////                    relEntity.setCreate_by("system");
////                    relEntity.setCreate_time(new Date());
////                    managementRelEntities.add(relEntity);
////                }
//
//            }
//
//            // 批量保存数据
////            tenderInformationService.saveBatch(entities);
//            tenderInformationKeywordManagementRelService.saveBatch(managementRelEntities);
//
//        } catch (IOException e) {
//            e.printStackTrace();
//        } catch (ParseException e) {
//            throw new RuntimeException(e);
//        }
//        return Result.ok("读取成功");
//    }
//
//    @Override
//    public Map<String, Long> getCacheSecurityKeywordManagement() {
//        if (redisUtil.hasKey(SECURITY_KEYWORD_MANAGE_CACHE)) {
//            String s = JacksonUtil.toJsonString(redisUtil.get(SECURITY_KEYWORD_MANAGE_CACHE));
//            return JacksonUtil.parseObject(s, new TypeReference<Map<String, Long>>() { });
//        } else {
//            Map<String, Long> security = super.list().stream().collect(Collectors.toMap(TenderSecurityKeywordManagementEntity::getKeywordName, TenderSecurityKeywordManagementEntity::getId));
//            // 缓存
//            redisUtil.set(SECURITY_KEYWORD_MANAGE_CACHE, security, SECURITY_KEYWORD_MANAGE_CACHE_EXPIRE);
//            return security;
//        }
//    }
//
//
//}